#include "mapwidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>

mapWidget::mapWidget(QWidget *parent)
    : QWidget{parent} {
    m_map = new myMap(myMap::FullType, "poi_data.json");
    m_view = new myView(this);
    // 添加道路（调试用）
    // connect(m_view, &myView::makePath, &m_map, &myMap::makePath);
    // connect(m_view, &myView::makePath, this, [this]() {
    //     setWindowTitle(QString::number(n));
    //     ++n;
    // });
    m_nodeInfo = new myNodeInfo(this);
    // m_map.function();    生成道路（调试用）
    m_view->drawPath(m_map);
    m_view->drawNode(m_map);
    connect(m_view, &myView::selected, this, &mapWidget::changeText);
    connect(m_view, &myView::selected, this, &mapWidget::addVisitingNodeButtonChange);
    QHBoxLayout *mainLayout = new QHBoxLayout;
    setLayout(mainLayout);
    QVBoxLayout *leftLayout = new QVBoxLayout;
    mainLayout->addLayout(leftLayout);
    leftLayout->addWidget(m_view);
    QHBoxLayout *infoLayout = new QHBoxLayout;
    leftLayout->addLayout(infoLayout);
    infoLayout->addWidget(m_nodeInfo);
    QVBoxLayout *buttonLayout = new QVBoxLayout;
    infoLayout->addLayout(buttonLayout);
    QVBoxLayout *rightLayout = new QVBoxLayout;
    mainLayout->addLayout(rightLayout);
    QScrollArea *rightScroll = new QScrollArea;
    rightScroll->setWidgetResizable(true);
    rightScroll->setMinimumWidth(350);
    rightLayout->addWidget(rightScroll);
    rightScroll->close();
    //待参观队列窗口
    m_visitingNodeWidget = new VisitingNodeWidget;
    connect(m_visitingNodeWidget, &VisitingNodeWidget::nodeUnitRemoved, this, &mapWidget::removeVisitingNode);
    rightScroll->setWidget(m_visitingNodeWidget);
    //将选中节点添加至待参观队列按钮
    m_addVisitingNodeButton = new QPushButton;
    m_addVisitingNodeButton->setText("请选择地点");
    m_addVisitingNodeButton->setEnabled(false); //将按钮设为不可点击（初始未选择节点）
    m_addVisitingNodeButton->setFixedSize(120, 50);
    connect(m_addVisitingNodeButton, &QPushButton::clicked, this, [this](){
        if (m_map->checkVisitingNode(m_curNode->getID()) == 0 ||
            m_map->checkVisitingNode(m_curNode->getID() == 2)) {
            m_map->addVisitingNode(m_curNode->getID());
            m_visitingNodeWidget->addNodeUnit(m_curNode);
            m_addVisitingNodeButton->setText("该地点已添加");
            m_addVisitingNodeButton->setEnabled(false);
        }
    });
    buttonLayout->addWidget(m_addVisitingNodeButton);
    //显示待参观地点按钮
    QPushButton *showVisNode = new QPushButton("显示待参观地点");
    showVisNode->setFixedSize(120, 50);
    connect(showVisNode, &QPushButton::clicked, this, [=]() {
        if (m_isVisitingOpen == false) {
            showVisNode->setText("关闭待参观地点");
            rightScroll->show();
            m_isVisitingOpen = true;
        } else {
            showVisNode->setText("显示待参观地点");
            rightScroll->close();
            m_isVisitingOpen = false;
        }
    });
    buttonLayout->addWidget(showVisNode);
    this->show();
}

myView *mapWidget::getView() const {
    return m_view;
}

void mapWidget::changeText(myNode *node) {
    m_nodeInfo->setID(node->getID());
    m_nodeInfo->setName(node->getName());
    m_nodeInfo->setAddress(node->getAddress());
    m_nodeInfo->setRating(node->getRating());
    m_nodeInfo->setRateNum(node->getRateNum());
}

void mapWidget::addVisitingNodeButtonChange(myNode *node) {
    m_curNode = node;
    switch(m_map->checkVisitingNode(node->getID())) {
    case 0:
    case 3:
        m_addVisitingNodeButton->setText("添加至待参观地点");
        m_addVisitingNodeButton->setEnabled(true);
        break;
    case 1:
        m_addVisitingNodeButton->setText("待参观地点已满");
        m_addVisitingNodeButton->setEnabled(false);
        break;
    case 2:
        m_addVisitingNodeButton->setText("该地点已添加");
        m_addVisitingNodeButton->setEnabled(false);
        break;
    }
}

void mapWidget::removeVisitingNode(int ID) {
    m_map->removeVisitingNode(ID);
    if (m_curNode->getID() == ID) {
        m_addVisitingNodeButton->setText("添加至待参观地点");
        m_addVisitingNodeButton->setEnabled(true);
    }
}

